package com.example.dllo.exe.me;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;

import android.provider.MediaStore;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.dllo.exe.R;
import com.example.dllo.exe.main.BaseFragment;

import java.util.ArrayList;

import cn.bmob.v3.BmobUser;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.UpdateListener;

/**
 * code is far away from bug with the animal protecting
 * <p>
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃永无BUG！  凯哥 祝你一路顺风
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 */
public class MeFragment extends BaseFragment implements View.OnClickListener {

    private ArrayList<String> list;
    private ArrayList<Fragment> fragments;

    private TabLayout tabLayout_me;
    private ViewPager viewPager_me;

    private LinearLayout linearLayout;

    private TextView textViewname;

    private ImageView imageViewhead;
    private ImageView imgsetting;

    private static int RESULT_LOAD_IMAGE = 1;

    private View enterView;
    private View noEnterView;
    private TextView exitEnter;
    private ImageView weiDengLu;
    private LinearLayout titleLl;
    private ImageView imageViewsetting;

    @Override
    protected int setLayout() {
        return R.layout.mefragmentlayout;
    }

    @Override
    protected void initView() {
        tabLayout_me = bindView(R.id.me_DanpinTablayout);
        viewPager_me = bindView(R.id.me_GonglueViewpager);
        imageViewsetting = bindView(R.id.me_setting);
        imageViewsetting.setOnClickListener(this);
        titleLl = bindView(R.id.me_fragment_title);
        titleLl.bringToFront();
        enterView = LayoutInflater.from(mContext).inflate(R.layout.mereplacelayout, null);
        imageViewhead = (ImageView) enterView.findViewById(R.id.meonclick_image);
        textViewname = (TextView) enterView.findViewById(R.id.text_name);
        imageViewhead.setOnClickListener(this);
        exitEnter = (TextView) enterView.findViewById(R.id.text_back);
        exitEnter.setOnClickListener(this);
        noEnterView = LayoutInflater.from(mContext).inflate(R.layout.meaddlayout, null);
        weiDengLu = (ImageView) noEnterView.findViewById(R.id.meonclick_weidenglu);
        weiDengLu.setOnClickListener(this);
        linearLayout = bindView(R.id.LL);



    }

    @Override
    protected void initData() {
        MeAdapter meAdapter = new MeAdapter(getChildFragmentManager(), getContext());
        list = new ArrayList<>();

        list.add("单品");
        list.add("攻略");
        fragments = new ArrayList<>();
        fragments.add(new MeDanpinFragment());
        fragments.add(new MeGonglueFragment());
        meAdapter.setFragments(fragments);
        meAdapter.setList(list);
        viewPager_me.setAdapter(meAdapter);
        tabLayout_me.setupWithViewPager(viewPager_me);
        tabLayout_me.setSelectedTabIndicatorColor(Color.RED);


    }

    @Override
    public void onResume() {
        super.onResume();
        MyUser myUser = MyUser.getCurrentUser(MyUser.class);
        if (myUser != null) {
            linearLayout.removeAllViews();
            linearLayout.addView(enterView);
            textViewname.setText(myUser.getUsername());


        } else {
            //removeAllViews()移除视图的方法
            linearLayout.removeAllViews();
            linearLayout.addView(noEnterView);
        }

    }

    //回调方法
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESULT_LOAD_IMAGE && resultCode == Activity.RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContext().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();
            Bitmap bitmap = BitmapFactory.decodeFile(picturePath);
            imageViewhead.setImageBitmap(bitmap);
            //获取user数据需要调动的方法
            MyUser myUser = BmobUser.getCurrentUser(MyUser.class);
            myUser.setIcon(bitmap);
            Bitmap bitmap1 = myUser.getIcon();
            CircleDrawable circleDrawable = new CircleDrawable(bitmap1);
            imageViewhead.setImageDrawable(circleDrawable);
            AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);
            alphaAnimation.setDuration(1000);
            imageViewhead.setAnimation(alphaAnimation);
            alphaAnimation.start();
            myUser.setIcon(bitmap1);
            //保存
            myUser.update(new UpdateListener() {
                @Override
                public void done(BmobException e) {
                    if (e != null) {
                        Log.d("MeFragment", "更新失败" + e.toString());
                    } else {
                        Log.d("MeFragment", "更新成功");
                    }
                }
            });


        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.meonclick_weidenglu:
                Intent intent = new Intent(getContext(), ZhuCeAty.class);
                startActivity(intent);
                break;
            case R.id.me_setting:
                Intent intent1 = new Intent(getContext(), SettingAty.class);
                startActivity(intent1);

                break;
            case R.id.meonclick_image:
                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                builder.setTitle("选择相册");
                builder.setMessage("你确定要更换相册么?");
                builder.setIcon(R.mipmap.giftphoto);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        //调用系统相册的方法
                        Intent b = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        startActivityForResult(b, RESULT_LOAD_IMAGE);
                    }
                });
                builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.setNeutralButton("忽略", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.show();


                break;
            case R.id.text_back:
                MyUser.logOut();
                linearLayout.removeAllViews();
                linearLayout.addView(noEnterView);
                break;

        }


    }


}